
package org.finesys.common.hazelcast.core.mq;

import com.hazelcast.topic.Message;
import com.hazelcast.topic.MessageListener;

import cn.hutool.core.util.TypeUtil;
import lombok.Getter;
import lombok.Setter;
import lombok.SneakyThrows;


@Getter
@Setter
public abstract class AbstractTopicMessageListener<T extends AbstractTopicMessage>
        implements MessageListener<T>, HazelcastMessageListener<T> {

    /**
     * 消息类型
     */
    private final Class<T> messageType;

    /**
     * topic
     */
    private final String topic;

    @SneakyThrows
    protected AbstractTopicMessageListener() {
        this.messageType = (Class<T>) TypeUtil.getTypeArgument(getClass(), 0);
        this.topic = messageType.newInstance().getTopic();
    }

    @Override
    public void onMessage(Message<T> message) {
        this.onMessage(message.getMessageObject());
    }

}
